/**
 * 
 */
package org.wattdepot.cli.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.junit.Test;


/**
 * @author George Lee
 *
 */
public class TestCarbonStatsCommand extends CommandTestBed {

  /**
   * Tests the doCommand method of the carbon stats command.
   */
  @Override
  @Test
  public void testDoCommand() {
    //Sad tests.
    //Correct invocation
    //carbonstats SIM_OAHU_GRID from 2009-11-01 to 2009-11-30 stat max
    CarbonStatsCommand carbonStats = new CarbonStatsCommand(this.client);
    String badParameter = "foo";
    
    String command = "carbonstats";
    String result = carbonStats.doCommand(command);
    assertFalse("Testing that command works.",
        result.contains(WattDepotCliCommand.UNKNOWN_COMMAND));
    assertTrue("Check that there are not enough parameters.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));
    
    command = "carbonstats SIM_OAHU_GRID foo 2009-11-01 to 2009-11-30";
    result = carbonStats.doCommand(command);
    assertFalse("Check that there are enough parameters.",
        result.contains(WattDepotCliCommand.PARAMETER_COUNT_ERROR));
    assertTrue("Check that we got a bad from parameter.", 
        result.contains(WattDepotCliCommand.PARAMETER_ERROR + badParameter));
    
    command = "carbonstats SIM_OAHU_GRID from 2009-11-01 foo 2009-11-30";
    result = carbonStats.doCommand(command);
    assertTrue("Check that we got a bad to parameter.", 
        result.contains(WattDepotCliCommand.PARAMETER_ERROR + badParameter));
    
    command = "carbonstats SIM_OAHU_GRID from foo to 2009-11-30";
    result = carbonStats.doCommand(command);
    assertTrue("Check that we got a bad from timestamp.", 
        result.contains(WattDepotCliCommand.TSTAMP_ERROR + badParameter));
    
    command = "carbonstats SIM_OAHU_GRID from 2009-11-01 to foo";
    result = carbonStats.doCommand(command);
    assertTrue("Check that we got a bad from timestamp.", 
        result.contains(WattDepotCliCommand.TSTAMP_ERROR + badParameter));
    
    // Happy tests
    command = "carbonstats SIM_OAHU_GRID from 2009-11-01 to 2009-11-10";
    result = carbonStats.doCommand(command);
    StringTokenizer tokenizer = new StringTokenizer(result);
    List<String> tokens = new ArrayList<String>();
    while (tokenizer.hasMoreTokens()) {
      tokens.add(tokenizer.nextToken());
    }
    assertEquals("Check that we got a valid max", tokens.get(0), "Max:");
    double value = Double.valueOf(tokens.get(1));
    assertTrue("Testing that we got a valid value.", value >= 0.0);
  }

}
